/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package compilador;

import java.io.IOException;
import java.io.Reader;

/**
 *
 * @author Melinton
 */
public class CharBuffer 
{
    //--------------------------------------------------------------------------
    // ATRIBUTOS
    //--------------------------------------------------------------------------    
    private int[] buf;
    private Reader f;
    private int k;
    
    //--------------------------------------------------------------------------
    // CONSTRUCTOR
    //--------------------------------------------------------------------------    
    public CharBuffer(Reader in, int m)
    {
        k = m; 
        buf = new int[k];
        f = in;
        try
        {
            for(int i = 0; i<k; i++)
            {
                buf[i] = f.read();
            }
        }
        catch (IOException io)
        {
            System.err.println(io.getMessage());
            System.exit(1);
        }
    }
    
    //--------------------------------------------------------------------------
    // METODOS
    //--------------------------------------------------------------------------    
    public int LA(int i)
    {
        if( i>=i && i<=k)
            return buf[i-1];
        return 0;
    }
    
    public void consume() 
    {        
        for (int i=0; i<k-1; i++) 
        {
            buf[i]=buf[i+1];
        }        
        try 
        {
            buf[k-1] = f.read();
            if ( buf[k-1]=='\r' )             
                buf[k-1] = f.read();       
        }
        catch (IOException io) 
        {
            System.err.println(io.getMessage());
            System.exit(1);
        }
    }            
}
